<?
require("../db/opendb.php");
require_once('magpie/rss_fetch.inc');
require("../functions.php");

function Submit_Articles($url, $feed_id, $tags, $category, $lat, $lng, $city, $state)
{

	$rss = fetch_rss( $url );
exit("--------");
	//Get all the channel info
	if($rss->channel['source_encoding'] != '')
	{
		$encoding = $rss->channel['source_encoding'];
	}
	else
	{
		$encoding = 'UTF-8';
	}
	
	echo "<pre>ss";print_r($rss->items);exit;
	//$maintitle = $rss->channel['title'];
	//$mainlink = $rss->channel['link'];
	//$maindesc = $rss->channel['description'];
	$maintitle = mb_convert_encoding($rss->channel['title'],'HTML-ENTITIES', $encoding);
	$mainlink = mb_convert_encoding($rss->channel['link'],'HTML-ENTITIES', $encoding);
	$maindesc = mb_convert_encoding($rss->channel['description'],'HTML-ENTITIES', $encoding);
	$feed_type = $rss->feed_type;
	$feed_version = $rss->feed_version;

	//echo $rss."<BR><BR>";
	//echo $mainlink."<BR><BR>";

	$insert = "Insert into Blog_Feeds (Url, Date_Created, Owner, Website, Title, Description, Category_ID, encoding, feed_type, feed_version, City, State, Latitude, Longitude) Values ('".$url."', '".date('Y-m-d')."', '".$_SESSION['id']."', '".$mainlink."', '".mysql_real_escape_string($maintitle)."', '".mysql_real_escape_string($maindesc)."', '".$category."', '".$encoding."', '".$feed_type."', '".$feed_version."', '".$city."', '".$state."', '".$lat."', '".$lng."')";
	echo $insert;
	$iresult = mysql_query($insert) or die ("SQL Error".mysql_error());
	$insert_id_feed = mysql_insert_id();

	$next_read = mktime(date('H')+2, date('i'), date('s'), date('m'), date('d'), date('Y'));
	$next_read = date('Y-m-d H:i:s', $next_read);

	$update = "update Blog_Feeds set Last_Read = '".date('Y-m-d H:i:s')."', Next_Read = '".$next_read."' where ID = '".$insert_id_feed."'";
	$iresult = mysql_query($update) or die ("SQL Error".mysql_error());

	if($tags !='')
	{
		$tags2 = explode(",", $tags);

		$i = 0;

		while($i < count($tags2))
		{
			$insert = "Insert into Blog_Tags (Blog_Feed_ID, Tag, Owner) Values ('".$insert_id_feed."', '".$tags2[$i]."', '".$_SESSION['id']."')";
			$iresult = mysql_query($insert) or die ("SQL Error".mysql_error());
			$i++;
		}
	}
	$i=0;

	//submit all the articles and categories

	if($insert_id_feed != "")
	{
		$count = 0;
		if(strtoupper($feed_type) == "ATOM")
		{
			$count = 1;
			foreach ($rss->items as $item)
			{
				if($item['atom_content'] == "")
				{
					$array = $item['content'];
					$content = $array['encoded'];
				}
				else
				{
					$content = $item['atom_content'];
				}


				if($item['author_name'] != "")
				{
					$author = $item['author_name'];
				}
				else
				{
					if($item['author'] != "")
					{
						$author = $item['author'];
					}
					else
					{
						$aarray = $item['dc'];
						$author = $aarray['creator'];
					}
				}

				$categories = $item['category'];
				$array = $item['dc'];
				$categories .= $array['subject'];


				$created = date('Y-m-d H:i:s', $item['date_timestamp']);

				$c = insert_articles($item['title'], $item['link'], $content, $created, mysql_real_escape_string($author), $categories, $encoding, $insert_id_feed);
				//echo "ATOM:<BR>TITLE: ".$item['title']."<BR>LINK: ". $item['link']."<BR>CONTENT: ".$content."<BR>DATE: ".$created."<BR>AUTHOR: ".$author."<BR>CATEGORIES: ".$item['category']."<BR><BR><BR>";
				$count++;
			}
		}

		if(strtoupper($feed_type) == "RSS" && $feed_version == "1.0")
		{
			$count = 0;
			foreach ($rss->items as $item)
			{

				$content = $item['description'];

				if($item['author'] != "")
				{
					$author = $item['author'];
				}
				else
				{
					$aarray = $item['dc'];
					$author = $aarray['creator'];
				}


				$categories = $item['category'];
				$array = $item['dc'];
				$categories .= $array['subject'];


				$created = date('Y-m-d H:i:s', $item['date_timestamp']);

				$c = insert_articles($item['title'], $item['link'], $content, $created, mysql_real_escape_string($author), $categories, $encoding, $insert_id_feed);
				//echo "RSS1<BR>TITLE: ".$item['title']."<BR>LINK: ". $item['link']."<BR>CONTENT: ".$content."<BR>DATE: ".$created."<BR>AUTHOR: ".$author."<BR>CATEGORIES: ".$item['category']."<BR><BR><BR>";
				$count++;
			}
		}

		if(strtoupper($feed_type) == "RSS" && $feed_version == "2.0")
		{
			$count = 0;
			foreach ($rss->items as $item)
			{

				if($item['atom_content'] == "")
				{
					$array = $item['content'];
					$content = $array['encoded'];
				}
				else
				{
					$content = $item['atom_content'];
				}
				if($item['description'] != "")
				{

					$content = $item['description'];
				}

				if($item['author_name'] != "")
				{
					$author = $item['author_name'];
				}
				else
				{
					if($item['author'] != "")
					{
						$author = $item['author'];
					}
					else
					{
						$aarray = $item['dc'];
						$author = $aarray['creator'];
					}

				}

				$categories = $item['category'];
				$array = $item['dc'];
				$categories .= $array['subject'];

				$created = date('Y-m-d H:i:s', $item['date_timestamp']);

				$c = insert_articles($item['title'], $item['link'], $content, $created, mysql_real_escape_string($author), $categories, $encoding, $insert_id_feed);
				//echo "RSS2<BR>TITLE: ".$item['title']."<BR>LINK: ". $item['link']."<BR>CONTENT: ".$content."<BR>DATE: ".$created."<BR>AUTHOR: ".$author."<BR>CATEGORIES: ".$item['category']."<BR><BR><BR>";
				$count++;
			}

		}
	}

	//$result = "Added ".$count." articles to the database.";

	$result = 1;
	return $result;


}

function insert_articles($title, $link, $description, $date, $author, $categories, $aencoding, $feed_id)
{
	if($date != "")
	{

		$insertquery = "insert into Blog_Articles (Link, Title, Blog_Date, Description, Feed_ID, Date_Created, Author) Values ('". mb_convert_encoding($link,'HTML-ENTITIES', $aencoding)."','".mysql_real_escape_string(mb_convert_encoding($title,'HTML-ENTITIES', $aencoding))."','".$date."','".mysql_real_escape_string(mb_convert_encoding($description,'HTML-ENTITIES', $aencoding))."','".$feed_id."','".date('Y-m-d H:i:s')."', '".$author."')";
		mysql_query($insertquery) or die ("SQL Error".mysql_error());
		$insert_id = mysql_insert_id();
		//echo $insertquery."<BR><BR>";

		//print_r($item['dc:subject']);
		$cats = ltrim($categories, "^");
		$cats = str_replace("^&^", " & ", $cats);

		//echo $cats."<BR><BR>";
		$cats = explode("^", $cats);
		foreach($cats as $cat)
		{
			if ($cat != "")
			{
				$insertquery = "insert into Blog_Article_Categories (Blog_Feed_ID, Category, Article_ID) Values ('".$feed_id."','".mysql_real_escape_string(mb_convert_encoding($cat,'HTML-ENTITIES', $aencoding))."', '".$insert_id."')";
				mysql_query($insertquery) or die ("SQL Error".mysql_error());
				//echo $insertquery."<BR><BR>";
			}
		}

		//add point to user account
		add_points('Blog_Articles', $_SESSION['id'], '10', 'Added Blog Post', $insert_id);

		$boolean = true;

	}
	return $boolean;
}
?>